<template>
  <Form class="xtx-form" :validation-schema="mySchema" v-slot="{errors}" ref="formCom">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field class="input" type="text" placeholder="请输入用户名" name="accounts" v-model="from.accounts"/>
      </div>
      <div class="error" v-if="errors.accounts">{{errors.accounts}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field class="input" type="text" placeholder="请输入手机号" name="mobile" v-model="from.mobile"/>
      </div>
      <div class="error" v-if="errors.mobile">{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field class="input" type="text" placeholder="请输入验证码"  name="code" v-model="from.code"/>
        <span class="code" @click="getIdentiFying()">{{timer===0? '发送验证码':`${timer}秒后重试`}}</span>
      </div>
      <div class="error" v-if="errors.code">{{errors.code}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field class="input" type="password" placeholder="请输入密码"  name="password" v-model="from.password"/>
      </div>
      <div class="error" v-if="errors.password">{{errors.password}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field class="input" type="password" placeholder="请确认密码" name="rePassword" v-model="from.rePassword"/>
      </div>
      <div class="error" v-if="errors.rePassword">{{errors.rePassword}}</div>
    </div>
    <a href="javascript:;" class="submit" @click="submit()">立即提交</a>
  </Form>
</template>

<script>
import { Field, Form } from 'vee-validate'
import { reactive, ref } from 'vue'
import { useStore } from 'vuex'
import schema from '@/utils/vee-validate-schema'
import { userQQpatch, finishUserQQpatch } from '@/api/user'
import { useIntervalFn } from '@vueuse/core'
import { useRouter, useRoute } from 'vue-router'
import Message from '@/components/library/message'
export default {
  name: 'CallbackPatch',
  components: {
    Field, Form
  },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const router = (useRouter())
    const route = (useRoute())
    const store = (useStore())
    const from = reactive({
      mobile: null,
      code: null,
      accounts: null,
      password: null,
      rePassword: null
    })
    const mySchema = {
      mobile: schema.mobile,
      code: schema.code,
      accounts: schema.accounts,
      password: schema.password,
      rePassword: schema.rePassword
    }
    // --------------------------------------------------------------

    // pause暂停，resume开始
    const timer = ref(0)
    const formCom = ref('null')
    const { pause, resume } = useIntervalFn(() => {
      console.log(123435)
      timer.value--
      if (timer.value === 0) {
        pause()
      }
    }, 1000, false)
    // 获取验证码
    const getIdentiFying = () => {
      // 验证手机号格式
      const valid = mySchema.mobile(from.mobile)
      console.log(valid)
      if (valid === true) {
        console.log(timer)
        if (timer.value === 0) {
          console.log('验证码发送成功')
          const { mobile } = from
          userQQpatch(mobile).then(data => {
            console.log(data.result)
            // 开始计时
            timer.value = 90
            resume()
            Message({ type: 'success', text: '验证码发送成功' })
          }).catch(e => {
            Message({ type: 'error', text: e.response.data.message })
          })
        }
        console.log(111)
      } else {
        // 使用vee库的错误函数显示错误信息，setFieldError(字段，错误信息)
        formCom.value.setFieldError('mobile', valid)
      }
    }
    // --------------------------------------------------------------------
    // 点击提交
    const submit = () => {
      finishUserQQpatch({ unionId: props.unionId, ...from }).then(data => {
        router.push(store.state.user.redirectUrl)
        console.log(111111)
        const { id, avatar, nickname, account, mobile, token } = data.result
        store.commit('user/setUser', { id, avatar, nickname, account, mobile, token })
        console.log(route.query.redirectUrl)
        router.push(store.state.user.redirectUrl)
        Message({ type: 'success', text: '完成登录' })
      }).catch(e => {
        if (e.response.data) {
          Message({ type: 'error', text: e.response.data.message })
        }
      })
    }
    // ------------------------
    return { from, mySchema, timer, getIdentiFying, formCom, submit }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
